<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 *
 *
 */
class Options extends CI_Model  {
	/**
	 * Constructor
	 * @access protected
	 */
	function _construct()
	{
      // Call the Model constructor
      parent::_construct();
	}
	function show($id)
	{

		$query="Select id,`idOption`,`Desc` from listoptions where idService=".$id;

		$result=$this->db->query($query);
		$responce->page=1;
		$i=0;
		foreach($result->result() as $row)
		{
			$query="Select name,rules from options where id=".$row->idOption;
			#$this->log($query);
			$res=$this->db->query($query);
			if($res->num_rows()>0)
			{
				$r2=$res->row();
				$responce->rows[$i]['id']="m".$row->id;
				$responce->rows[$i]['cell']=array($row->id,$r2->name,$row->Desc,$r2->rules);
				$i++;
			}
		}
		$this->db->close();
		return json_encode($responce);
	}
	/*function getList($type)
	{

		$query="Select id,name from options where idType=".$type;
		#$this->log($query);
		$result=$this->db->query($query);
		$responce=array();
		foreach($result->result() as $row)
		{
			$responce[]=$row->id.":".$row->name;
		}
		if(count($responce)>0)
		{
			$responce= implode(";",$responce);
		}
		else {
			$responce="";
		}
		$this->db->close();
		return $responce;
	}*/

	function add($ServId,$name)
	{

		$query="Insert into listoptions(`idService`,`idOption`) values ('$ServId','$name')";
		#$this->log($query);
		$this->db->query($query);
		$this->db->close();
	}

	function del($ServId,$id)
	{

		$id=substr($id,1);
		$query="Delete from listoptions where id=".$id;
		#$this->log($query);
		$this->db->query($query);
		$this->db->close();
	}

	function edit($ServId,$Id,$desc)
	{
		$Id=substr($Id,1);
		$query="UPDATE listoptions SET `desc`='$desc' where id=".$Id;
		#$this->log($query);
		$this->db->query($query);
		$this->db->close();
	}
	function getList($type,$id)
	{
		$query="select `name`,`rules` from options where id=".$type;
		#$this->log($query);
		$qOpt=$this->db->query($query);
		if($qOpt->num_rows()>0)
		{
			$rule=$qOpt->row()->rules;
			$desc=$qOpt->row()->name;
		}
		else
		{
			#$this->log("error in query = ".$query);
			exit();
		}
		$responce=array();
		switch($rule)
		{
			case strpos($rule,'list-'):
				#$this->log($rule." list-");
				$responce['type']='list';
				$idS= substr($rule,5);
				#$this->log("idS=".$idS);
				$query="select `desc` from listoptions where idService=$id and idOption=$type";
				#$this->log($query);
				$seted=$this->db->query($query)->result_array();
				$options=array();
				foreach($seted as $row)
				{
					$options[]=$row['desc'];
				}
				if($idS!="")
				{
					$query="Select id,name from service where idtype=$idS and idUser=".$this->session->userdata('idUser');
					#$this->log($query);
					$list=$this->db->query($query);
					
					foreach($list->result() as $row)
					{
						$responce['data'][$row->id]['name']=$row->name;
						if(in_array($row->id,$options)){
							$responce['data'][$row->id]['checked']=true;
						}
						else{
							$responce['data'][$row->id]['checked']=false;
						}
					}
				}
			break;
			case strpos($rule,'int'):
				#$this->log($rule." int");
				$responce['type']='int';
				$query="select `desc` from listoptions where idService=$id and idOption=$type";
				$seted=$this->db->query($query)->result_array();
				$options=array();
				foreach($seted as $row)
				{
					$options[]=$row['desc'];
				}
				$responce['data']=$options;
			break;
			case strpos($rule,'string'):
				$responce['type']='string';
				#$this->log($rule." string");
				$query="select `desc` from listoptions where idService=$id and idOption=$type";
				$seted=$this->db->query($query)->result_array();
				$options=array();
				foreach($seted as $row)
				{
					$options[]=$row['desc'];
				}
				$responce['data']=$options;
			break;
			case strpos($rule,'option['):
				$responce['type']='option';
				#$this->log($rule." option[");
				$pattern = "#option\[(.*?)\]#i";
				preg_match($pattern,$rule,$list);
				#$this->log("list[0] ".$list[0]);
				#$this->log("list[1] ".$list[1]);
				$this->log('Start option');
				$this->log('LIST');
				$this->log($list);
				$alist=explode(',',$list[1]);
				$this->log('ALIST');
				$this->log($alist);
				$query="select `desc` from listoptions where idService=$id and idOption=$type";
				$this->log($query);
				$seted=$this->db->query($query)->result_array();
				$this->log($seted);
				$option=array();
				foreach($seted as $row)
				{
					$option[]=$row['desc'];
				}
				
				foreach($alist as $row)
				{
					$responce['data'][$row]['name']=$row;
					$responce['data'][$row]['checked']=false;
					$f=0;
					foreach($option as $it)
					{
						if($row==$it)
						{
							$responce['data'][$row]['checked']=true;
						}
					}
					
				}
				#$this->log('End option');
			break;
			case strpos($rule,'list['):
				$pattern = "#list\[(.*?)\]#i";
				preg_match($pattern,$rule,$list);
				switch($list[1])
				{
					case 'h':
						$query="select id,name,rules from options where rules like '%h'";
						$result=$this->db->query($query);
						foreach($result->result() as $row)
						{
							$pattern = "#\[(.*?)\]h#i";
							preg_match($pattern,$row->rules,$list);
							#print_r($list);
							#print_r($row->rules);
							$responce['data'][$row->id]['name']=$list[1];
							$responce['data'][$row->id]['desc']=$row->name;
							$responce['data'][$row->id]['checked']=false;
						}
						$query="select `desc` from listoptions where idService=".$id." and idOption=".$type;
						$setedList=$this->db->query($query);
						foreach ($setedList->result() as $row)
						{
							$responce['data'][$row->desc]['checked']=true;
						}
					break;
					case 'hr':
						$query="select id,name,rules from options where rules like '%hr'";
						$result=$this->db->query($query);
						foreach($result->result() as $row)
						{
							$pattern = "#\[(.*?)\]hr#i";
							preg_match($pattern,$row->rules,$list);
							$responce['data'][$row->id]['name']=$list[1];
							$responce['data'][$row->id]['desc']=$row->name;
							$responce['data'][$row->id]['checked']=false;
						
						}
						$query="select `desc` from listoptions where idService=".$id." and idOption=".$type;
						$setedList=$this->db->query($query);
						foreach ($setedList->result() as $row)
						{
							$responce['data'][$row->desc]['checked']=true;
						}
					break;
				}
			break;
		}
		
		/*$query="select `desc` from listoptions where idService=$id and idOption=$type";
		#$this->log($query);
		$seted=$this->db->query($query)->result_array();
		$options=array();
		foreach($seted as $row)
		{
			$options[]=$row['desc'];
		}
		$query='select rules from options where id='.$type;
		#$this->log($query);
		$option=$this->db->query($query);
		if($option->num_rows()>0)
		{
			$idS=$option->row()->rules;
			#$this->log(" idS ".$idS);
			if($idS!="")
			{
				$query="Select id,name from service where idtype=$idS and idUser=".$_SEdSSION['idUser'];
				#$this->log($query);
				$list=$this->db->query($query);
				$responce=array();
				foreach($list->result() as $row)
				{
					$responce['data'][$row->id]['name']=$row->name;
					if(in_array($row->id,$options)){
						$responce['data'][$row->id]['checked']=true;
					}
					else{
						$responce['data'][$row->id]['checked']=false;
					}
				}
			}
			else
			{
				$responce['data']=$options;
			}
		}*/
		if(count($responce)>0)
		{
			$responce['state']=true;
		}
		else
		{
			$responce['state']=false;
		}
		
		return json_encode($responce);
		
		}
	function saveoptions($type,$id,$val,$opt)
	{
		print_r($val);
		foreach($val as $type=>$values)
		{
			if(is_array($values))
			{
				$this->log("ARRAY");
				$query="delete from listoptions where idService=".$id." and idOption=".$type." ";
				$this->log($query);
				$this->db->query($query);
			
				foreach($values as $key=>$value)
				{
					$this->log($key." ".$value);
					if($key!='0')
					{
						$this->log($key." ".$value);
						if($value!='on')
						{
								$query="insert into listoptions (`idService`,`idOption`,`desc`) values (".$id.",".$type.",'".$value."')";
								$this->log($query);
								$this->db->query($query);
							
						}
						else 
						{
							
							$query="insert into listoptions (`idService`,`idOption`,`desc`) values (".$id.",".$type.",'".$key."')";
							$this->log($query);
							$this->db->query($query);
						}
					}
				}
				$this->log("END ARRAY");
			}
			else
			{
				$query="Delete from listoptions where idService=".$id." and idOption=".$type;
				$this->log("wtf ".$query);
				$this->db->query($query);
				$query="insert into listoptions (`idService`,`idOption`,`desc`) values (".$id.",".$type.",'".$values."')";
				$this->log($query);
				$this->db->query($query);
			}
		}
	}
}